package com.community.process._1010.event.push;

import com.community.process._1010.common.api.CommonResult;
import com.community.process._1010.common.utils.ExternalApiUtils;
import com.community.process._1010.dao.entity.ProcessRefundOrder;
import com.community.process._1010.dao.entity.ProcessTOrder;
import com.community.process._1010.dao.service.ProcessRefundOrderIService;
import com.community.process._1010.event.RefundOrderEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class RefundOrderPush {

    private final ProcessRefundOrderIService processRefundOrderIService;

    @Async
    @EventListener(classes = RefundOrderEvent.class)
    public void refundOrderEvent(RefundOrderEvent event) {
        if (event.getOrder() != null) {
            ProcessTOrder order = event.getOrder();
            ProcessRefundOrder refundOrder = new ProcessRefundOrder(order);
            // 走退款逻辑
            CommonResult<Boolean> result = ExternalApiUtils.agreeRefundUrl(order.getTradeNo(), event.getDto());
//            ErrorAssert.notNull(result.getData(),result.getMsg());
            if (result.getData() != null && result.getData()) {
                refundOrder.setRefundRemarks("退款成功");
                refundOrder.setIsSuccess(1);
            } else {
                refundOrder.setRefundRemarks(result.getMsg());
                refundOrder.setIsSuccess(0);
            }
            this.processRefundOrderIService.save(refundOrder);
        }
    }
}
